<?php
// ----------------------------------------------------------
// * @Copyright @ 2011-2020 By HuaXiaXinCai
// ----------------------------------------------------------
// * @Author Yang Changwu<800334@qq.com> 2016-12-12
// ----------------------------------------------------------
namespace Controller;
use \GatewayWorker\Lib\Gateway;
use \Controller\Core;

class Activity extends Core{
    //获取大转盘信息和排行
    public function Roulette($data,$client_id){
        global $globalinfo;
        //获取转盘排行
        //$ranking = $this->db->select('username,activity_name')->from('sz_activity_log')->where("activity_type=1 and awardtype!=7")->orderByASC(array('addtime'),false)->limit(10)->query();
        $rankingsql = "SELECT b.nickname as username,a.activity_name FROM `sz_activity_log` as a JOIN sz_user as b where a.username = b.username and  a.activity_type=1 and a.awardtype!=7 order by id desc LIMIT 10";
        $ranking = $this->db->query($rankingsql);
        $msg = [
            'method' => 'Roulette',
            'activity' => $globalinfo['activity_roulette'],
            'ranking' => $ranking,
            'num' => $_SESSION['roulette'],
        ];
        return self::SendMsg(1001,$msg);
    }
    //大转盘中奖
    public function RouletteAward($data,$client_id) {
        global $globalinfo;
        echo "\n================".$_SESSION['username']." 转盘次数:".$_SESSION['roulette']."==============\n";
        if($_SESSION['roulette'] == 0){
            //判断金币
            $user = $this->db->select('gold')->from('sz_user')->where('uid='.$_SESSION['uid'])->row();
            if(($user['gold']-10) < 0){
                return self::SendMsg(1023,array('method'=>'RouletteAward'));
            }
            //减掉金币
            $sqluser = "UPDATE `sz_user` SET `gold` = gold-10 WHERE uid=".$_SESSION['uid'];
            $this->db->query($sqluser);
            
            $sqluser = "UPDATE `sz_user` SET roulette=roulette+1 WHERE uid=".$_SESSION['uid'];
            $this->db->query($sqluser);
            $_SESSION['roulette'] = $_SESSION['roulette']+1;
            //金币消费日志
            self::GoldLog(10, 2,'购买大转盘一次');
        }
        
        //减掉转盘次数
        $sqluser = "UPDATE `sz_user` SET roulette=roulette-1 WHERE uid=".$_SESSION['uid'];
        $this->db->query($sqluser);
        $_SESSION['roulette'] = $_SESSION['roulette']-1;
        
        $roulette_list = $this->db->select('activity_type,activity_id')->from('sz_activity_log')->where("activity_type=1 and username='".$_SESSION['username']."' and activity_id not in ('4,5,6,7,14')")->orderByASC(array('id'),false)->limit(2)->query();
        if($roulette_list){
           if(count($roulette_list) ==1){
               $activity_award_number = 7;
           }else{
               if($roulette_list[0]['activity_id'] == 8 && $roulette_list[1]['activity_id'] == 8){
                   $activity_award_number = 0;
               }else{
                   $activity_award_number = 7;
               }
           }
        }else{
            $activity_award_number = 0;
        }
        
        
        //购买记录 50次 增加一次大奖机会
        $gold_log = $this->db->select('count(id) as sum')->from('sz_gold_log')->where("type=2 and purpose='购买大转盘一次'")->row();
        if($gold_log['sum'] % 20 == 0  && $gold_log['sum'] != 0){
            /*unset($globalinfo['activity_roulette'][2]);
            unset($globalinfo['activity_roulette'][3]);
            unset($globalinfo['activity_roulette'][4]);
            unset($globalinfo['activity_roulette'][5]);
            unset($globalinfo['activity_roulette'][7]);
            unset($globalinfo['activity_roulette'][8]);
            $activity_award_number = array_rand($globalinfo['activity_roulette'],1);*/
            $activity_award_number = 1;
        }
        
        //写入转盘奖励信息
        $activity = $globalinfo['activity_roulette'][$activity_award_number];
        $award = [
            'username'=> $_SESSION['username'],
            'activity_type'=> 1,
            'activity_id'=> $activity['id'],
            'activity_name'=> $activity['name'],
            'awardtype'=> $activity['awardtype'],
            'awardgift'=> $activity['awardgift'],
            'addtime'=> time(),
            'status' =>1,
        ];
        $onlineaward_log['id'] = $this->db->insert('sz_activity_log')->cols($award)->query();
        
        //增加奖励金币
        if($award['awardtype'] == 2){
            $gold = $award['awardgift'];
            //增加金币
           $sqluser = "UPDATE `sz_user` SET `gold` = gold+$gold WHERE uid=".$_SESSION['uid'];
           $this->db->query($sqluser);
            //金币增加日志
            self::GoldLog( $gold, 1,'大转盘获得');
        }
        //增加奖励再来一次
        if($award['awardtype'] == 6){
            //增加转盘次数
            $sqluser = "UPDATE `sz_user` SET roulette=roulette+1 WHERE uid=".$_SESSION['uid'];
            $this->db->query($sqluser);
            $_SESSION['roulette'] = $_SESSION['roulette']+1;
        }
        
        $msg = [
            'method' => 'RouletteAward',
            'awardnum' =>$activity_award_number+1,
            'num' => $_SESSION['roulette'],
        ];
        return self::SendMsg(1001,$msg);
    }
    //大转盘付费
    public function RoulettePay($data,$client_id) {
        //判断金币
        $user = $this->db->select('gold')->from('sz_user')->where('uid='.$_SESSION['uid'])->row();
        if(($user['gold']-10) < 0){
            return self::SendMsg(1023,array('method'=>'RoulettePay'));
        }
        //减掉金币
        $sqluser = "UPDATE `sz_user` SET `gold` = gold-10 WHERE uid=".$_SESSION['uid'];
        $this->db->query($sqluser);
        
        self::GoldLog(10, 2,'购买大转盘');

        return self::SendMsg(1001,array('method'=>'RoulettePay','num'=>1));
    }
    //在线奖励信息
    public function Online($data,$client_id){
        if(!isset($data['type'])) $data['type'] = 2;
        //读取在线奖励信息
        $OnlineActivity = $this->db->select('id,name,conditionvalue,awardtype,awardgift')->from('sz_activity')->where("type={$data['type']} and status=1")->query();
        //读取用户每日在线时长
        $user = $this->db->select('dayline')->from('sz_user')->where('uid='.$_SESSION['uid'])->row();
        //读取用户奖励列表
        $today1 = strtotime(date('Y-m-d',time()) . ' 00:00:00');
        $today2 = strtotime(date('Y-m-d',time()) . ' 23:59:59');
        $onlineaward = $this->db->select('id,username,activity_type,activity_id,status')->from('sz_activity_log')->where("username='".$_SESSION['username']."' and activity_type=2 and addtime between $today1 and $today2")->query();

        //合并在线奖励,判断奖励是否被领取
        $OnlineActivity_arr = [];
        foreach ($OnlineActivity as $value) {
            foreach ($onlineaward as $val) {
                if($val['activity_id'] == $value['id']){
                    $value['award_id'] = $val['id'];
                    $value['status'] = $val['status'];
                }
            }
            array_push($OnlineActivity_arr, $value);
        }
        $msg = [
            'method' => 'Online',
            'data' => $OnlineActivity_arr,
            'online'=> $user['dayline'],
        ];
        return self::SendMsg(1001,$msg);
    }
    //领取在线奖励
    public function OnlineAward($data,$client_id){
        if(!$data['award_id']){
            return self::SendMsg(1003,['method'=>'OnlineAward']);
        }
        $award = $this->db->select('id,awardgift')->from('sz_activity_log')->where("id=".$data['award_id'])->row();
        if($award){
            if($award['status'] == 1){
                return self::SendMsg(1026,['method'=>'OnlineAward']);
            }else{
                //增加金币
                $gold = $award['awardgift'];
                $sqluser = "UPDATE sz_user SET `gold` = gold+$gold WHERE uid=".$_SESSION['uid'];
                $this->db->query($sqluser);
                //改变在线奖励的状态
                $sqlaward = "UPDATE sz_activity_log SET status=1 WHERE id=".$award['id'];
                $this->db->query($sqlaward);
                return self::SendMsg(1001,['method'=>'OnlineAward','gold'=>$gold,'award_id'=>$data['award_id']]);
            }
        }else{
            return self::SendMsg(1025,['method'=>'OnlineAward']);
        }
    }
}